package com.briup.prs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.reduce.IntSumReducer;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

/**
 * 统计共现次数
 */
public class Step4 extends Configured implements Tool {
//    ShopA ShopB
    public  static  class  Step4Mapper extends Mapper<LongWritable, Text,Text, IntWritable>{
        private final  IntWritable I=new IntWritable(1);
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] shopAAndShopB = value.toString().split("[\t]");
        String outKey=shopAAndShopB[0]+":"+shopAAndShopB[1];
        context.write(new Text(outKey),I);

    }
}
    @Override
    public int run(String[] args) throws Exception {
        Configuration conf = getConf();
        String in = conf.get("in");
        String out = conf.get("out");


        Job job = Job.getInstance(conf);
        job.setJobName("Step4");
        job.setJarByClass(this.getClass());

        TextInputFormat.addInputPath(job,new Path(in));
        job.setMapperClass(Step4Mapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        TextOutputFormat.setOutputPath(job,new Path(out));


        return job.waitForCompletion(true)?0:-1;
    }

    public static void main(String[] args) throws Exception {
        ToolRunner.run(new Step4 (),args);

    }
}
